package com.sunday.volunteer;

import android.app.Application;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.google.gson.Gson;
import com.sunday.common.logger.Logger;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.common.MemberConst;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.http.HttpClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2016/2/23.
 */
public class BaseApp extends Application implements Thread.UncaughtExceptionHandler {


    private static BaseApp instance;
    public static boolean isFinish = true;
    private String tempcoor = "bd09ll";
    public LocationClient mLocationClient;
    public MyLocationListener mMyLocationListener;
    private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
    public synchronized static BaseApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Logger.init("Sunday").hideThreadInfo().setMethodCount(3).setMethodOffset(2);
        Thread.setDefaultUncaughtExceptionHandler(this);
        SDKInitializer.initialize(getApplicationContext());

        mLocationClient = new LocationClient(this.getApplicationContext());
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
        initLocation();
        mLocationClient.start();



//        PlatformConfig.setWeixin("wx1a168e2a356daff7", "07ea33ce5024666f652f08e07b4fcff0");
//        //微信 appid appsecret
//        PlatformConfig.setSinaWeibo("2677357747", "4c59c175641455b47720af2c4edcbc28");
//        //新浪微博 appkey appsecret
//        PlatformConfig.setQQZone("1105786600", "At0gv0AicT7AXVuv");
//        Config.REDIRECT_URL = "http://sns.whalecloud.com/sina2/callback";

       // PushManager.getInstance().initialize(this.getApplicationContext());
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(tempMode);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType(tempcoor);//可选，默认gcj02，设置返回的定位结果坐标系，
        int span = 1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        mLocationClient.setLocOption(option);
    }

    /**
     * 实现实时位置回调监听
     */
    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location.getLatitude() != 0) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LATITUDE, String.valueOf(location.getLatitude()));
            }
            if (location.getLongitude() != 0) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LONGITUDE, String.valueOf(location.getLongitude()));
            }
            if (!TextUtils.isEmpty(location.getAddrStr())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCATION_ADDRESS, location.getAddrStr());
            }
            if (!TextUtils.isEmpty(location.getProvince())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCATION_PROVINCE, location.getProvince());
            }
            if (!TextUtils.isEmpty(location.getCity())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCALTION_CITY, String.valueOf(location.getCity()));
            }
            if (!TextUtils.isEmpty(location.getDistrict())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCALTION_DISTRCT, String.valueOf(location.getDistrict()));
            }
            mLocationClient.stop();
        }


    }


    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        if(!BuildConfig.DEBUG){
            System.exit(0);
        }
    }


    public long getMemberId() {
        long memberId = SharePerferenceUtils.getIns(this).getLong(MemberConst.MEMBERID, -1);
        return memberId;
    }

    public void setMemberId(long memberId) {
        SharePerferenceUtils.getIns(this).putLong(MemberConst.MEMBERID, memberId);
    }

    public Member getMember(){
        String memberString = SharePerferenceUtils.getIns(instance).getOAuth();
        if(TextUtils.isEmpty(memberString)){
            return null;
        }
        Gson gson = new Gson();
        try {
            Member member = gson.fromJson(memberString , Member.class);
            return member;
        }catch (Exception e){
            return null;
        }
    }






}
